package com.nabisoft.tutorials.mavenstruts.service;

import com.nabisoft.tutorials.mavenstruts.ejb.DepartmentFacade;
import com.nabisoft.tutorials.mavenstruts.entity.Department;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import org.apache.log4j.Logger;

import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/message")
@Stateless
public class MessageService {
    
    @EJB
    private DepartmentFacade dp;
    
    private static final Logger LOGGER = Logger.getLogger(MessageService.class);
    
    @GET
    @Path("ping")
    public String getServerTime() {
        LOGGER.debug("RESTful Service 'MessageService' is running ==> ping");
        return "received ping on "+new Date().toString();
    }
    
    @GET
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public List<Department> getAllMessages() throws Exception{
        List<Department> messages = null;
            
        //we could also use JNDI lookup of our EJB
        //MessageServiceBean msgService = (MessageServiceBean)new InitialContext().lookup("java:global/struts2-module-ear/struts2-module-ejb/MessageServiceBean");
        messages = dp.findAll();
        
        LOGGER.debug("getAllMessages(): found "+messages.size()+" message(s) on DB");
        
        return messages; //do not use Response object because this causes issues when generating XML automatically
    }
      
}